package com.bestcem.xm.award.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/11/8 10:34
 */
@Configuration
@EnableAsync
public class AwardThreadPoolConfig {

    /**
     * 可丢弃任务的线程池，用来做可有可无的任务
     * @return org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
     * @author xa.zhang
     * @date 2021/11/8 10:36
     */
    @Bean(name = "threadPoolDiscardPolicy")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setKeepAliveSeconds(30);
        //核心线程池数
        pool.setCorePoolSize(5);
        //最大线程
        pool.setMaxPoolSize(10);
        //队列容量
        pool.setQueueCapacity(10);
        //队列满，线程被拒绝执行策略
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        pool.setThreadNamePrefix("-discard-pool-");
        return pool;
    }

}
